<?php
declare(strict_types=1);
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 11:18
 */

namespace Modules\SmallApi\Service;

use HiCommon\Repository\CouponGrantDetailRepository;
use HiCommon\Repository\CouponsRepository;
use HiCommon\Repository\UsersRepository;
use HiCommon\Repository\OrderRepairstoreRepository;
use HiCommon\Repository\OrderCheckpointRepository;

class CouponshService extends BaseService
{

	/**
	 * 	商户APP扫码用户提供的二维码获得参数 ， 并携带参数请求优惠券、用户信息
	 *	hinq 2019 10 24 
	 *	$ticket_num string   领取优惠券时生成的唯一优惠券券号
	 *  扫码只能使用洗车券（pid为1）
	 **/
	public function coupon_detail( string $ticket_num = null ){

		try {
			if( empty( $ticket_num ) ){
				return ['state'=> 1,'msg'=>'参数错误','data'=>[],'error'=>''];
			}
			$arr_where = ['ticket_num'=>$ticket_num , 'status'=>1,'pid'=>'1'];
			$arr_option['field'] = ['id','user_id','coupon_id','ticket_num','over_at'];
			$arr_option['with'] = ['getUserInfo','getCouponInfo'];
			$arr_list = CouponGrantDetailRepository::get_one_where( (array)$arr_where , (array) $arr_option )->toArray();
			if( count( $arr_list ) < 1 ){
				return ['state'=> 1,'msg'=>'参数错误','data'=>[],'error'=>''];
			}
			//优惠券的过期时间  >   当前时间
			if( $arr_list['over_at'] > time() ){
				return ['state'=> 1,'msg'=>'该优惠券已过期，不可使用','data'=>[],'error'=>''];
			}

			return ['state'=>0 ,'msg'=>'ok','data'=>$arr_list,'error'=>''];
		} catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
	}



	/**
	 *	使用扫码扫出来的优惠券
	 *	hinq 2019 10 24
	 *
	 ***/
	public function coupon_use( int $nId = null ){
		try {
			if( empty( $nId ) ){
				return ['state'=>1 ,'msg'=>'参数错误','data'=>[],'error'=>''];
			}
			//获取当前优惠券的信息
			$arr_info = CouponGrantDetailRepository::get_one( (int) $nId )->toArray();
			if( empty( $arr_info ) ){
				return ['state'=>1 ,'msg'=>'参数错误','data'=>[],'error'=>''];
			}
			if( $arr_info['status'] != 1 ){
				return ['state'=>1 ,'msg'=>'该优惠券已过期或已使用。','data'=>[],'error'=>''];
			}
			if( $arr_info['over_at'] < time() ){
				return ['state'=>1 ,'msg'=>'该优惠券已过期，不可使用。','data'=>[],'error'=>''];
			}
			
			$data['id'] = $nId;
			$data['repairstore_id'] = 1;
			$data['status'] = 2;
			$data['use_msg'] = '商户app扫码使用优惠券';
			$data['use_type'] = 1;
			// dd( $data );
			$res = CouponGrantDetailRepository::edit_one( (array) $data );
			$result = hi_check_res($res,'edit_one');
	        if ($result['state'] != 0) {
	            return ['state'=> 1,'msg'=>'使用失败','data'=>[],'error'=>''];
	        }
	        return ['state'=> 0,'msg'=>'使用成功','data'=>[],'error'=>''];
		} catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
	}




	/**
	 *	商户获取已在该商户商用的优惠券列表
	 *	hinq 2019 10 24
	 *	只获取已经使用的优惠券
	 **/
	public function use_coupon_list( array $arr_search = [] ){

		try {
			$repairstore_id = 1;  //当前商户的商户id
			$arr_option['where'] = [ 'repairstore_id'=> $repairstore_id ,'status' => 2 ];
			$arr_option['with'] = ['getUserInfo','getCouponInfo'];
			$arr_option['field'] = ['id','user_id','coupon_id','ticket_num','status','updated_at'];
			$arr_list = CouponGrantDetailRepository::get_list( (array) $arr_search , (array) $arr_option )->toArray();
			$data = $arr_list['data'];
			$pids = [1=>'洗车券',2=>'满减券',3=>'折扣券'];
			foreach ( $data as $key => &$value) {
				if( $value['pid'] == 1 ){
					$value['get_coupon_info']['pid_msg'] = $pids[$value['pid']];
				}else if( $value['pid'] == 2 ){
					$value['get_coupon_info']['pid_msg'] = '满'.floor($value['get_coupon_info']['price_all']).'元减'.floor( $value['get_coupon_info']['price']).'元';
				}else if( $value['pid'] == 3 ){
					$value['get_coupon_info']['pid_msg'] = '满'.floor( $value['get_coupon_info']['price_all'] ).'元打'.floor( $value['get_coupon_info']['discount_num']).'折';
				}
			}
			$temp = [
				'data' => $data,
				'page'=>[
		            'current_page'=>$arr_list['current_page'],
		            'last_page'=>$arr_list['last_page'],
		            'per_page'=>$arr_list['per_page'],
		            'total'=>$arr_list['total'],
	          	],
			];
			return ['state'=> 0,'msg'=>'ok','data'=>$temp,'error'=>''];	
		} catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
	}


	/**
	 *	统计当前商户下已购买商品用户的订单统计
	 *	hinq 2019 10 25
	 *
	 **/
	public function user_to_order_count( array $arr_search ){

		try {
			//统计服务订单数据
			$shop_id = 16;
			$arr_where = ['shop_id'=>$shop_id];  //只获取每个商家数据  已支付的订单 已完成-订单关闭
			$arr_list[] = OrderRepairstoreRepository::get_user_to_order_count( (array) $arr_where )->toArray();

			// $arr_where = ['shop_id'=>14];  //只获取当前商家数据  已支付的订单 预约成功 检测成功的
			//统计服务订单
			// $arr_list[] = OrderCheckpointRepository::get_user_to_order_count( (array) $arr_where )->toArray();

			return ['state'=> 0,'msg'=>'ok','data'=>$arr_list,'error'=>''];
		}catch ( \Exception $exception){
			return  ['state'=>1,'msg'=>$exception->getMessage()];
		}



	}

}